package service;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import java.io.IOException;
import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/7/23.
 * count bit x in 1~n
 */
public class CrawlInfoService {


    public long startTime = System.currentTimeMillis();

    /**
     * 获取最新的公告信息
     *
     * @return
     * @throws IOException
     */
    public static List<String> stockNotice() throws IOException {

        List<String> list = new ArrayList<String>();

        String url = "http://www.sse.com.cn/disclosure/listedinfo/announcement/s_docdatesort_desc_2016openpdf.htm";
        Document doc = Jsoup
                .connect(url)
                .userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)")
                .timeout(5000).get();

        Elements dds = doc.getElementsByTag("dd");

        Iterator<Element> iterator = dds.iterator();

        while (iterator.hasNext()) {
            Element element = iterator.next();
            String date = element.getElementsByTag("span").get(0).html();
            String content = element.getElementsByTag("em").get(0).getElementsByTag("a").html();
            list.add(date + " ## " + content);
        }

        return list;
    }

    /**
     * 过滤公告信息
     *
     * @return
     */
    public static String filterNotice(List<String> list) {
        StringBuffer str = new StringBuffer();

        String[] filters = new String[]{
                "增发", "增持", "合作", "定增", "重组", "合同", "增长", "预增"
        };

        for (String s : list) {
            boolean flag = false;
            //是否包含某个关键字
            for (String s0 : filters) {
                if (s.contains(s0)) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                str.append(s + "\n");
                flag = false;
            }

        }

        return str.toString();
    }


    public static void main(String[] args) throws Exception {
        CrawlInfoService an = new CrawlInfoService();
        final List<String> infoList = stockNotice();
        final String out = filterNotice(infoList);
        System.out.println(out);

        System.out.println("\ntotal time consume: " + (System.currentTimeMillis() - an.startTime) + " ms.");
    }


}
